feat(package_info_plus): add opt-in compile-time package info accessor#3874
feat(package_info_plus): add opt-in compile-time package info accessor#3874Ortes wants to merge 2 commits into
Conversation
Adds an opt-in library, package_info_plus_environment.dart, exposing PackageInfoEnvironment.packageInfo. It returns the *running* app's PackageInfo: on web from compile-time PACKAGE_INFO_PLUS_* dart-defines, on every other platform by delegating to PackageInfo.fromPlatform(). On web there is no reliable runtime source for the running version: version.json is fetched from the server, so it reflects the deployed version rather than the executing (possibly cached) bundle, and the fetch can fail entirely (offline, CORS, hosting rewrites). The compile-time constants are embedded in the bundle and cannot diverge from it. A web build that omits PACKAGE_INFO_PLUS_VERSION fails to compile, so a misleading version can never ship silently. Native builds are unaffected. PackageInfo.fromPlatform() is unchanged and the new library is not exported by the package barrel, so this is purely additive and opt-in. Related: fluttercommunity#2675, fluttercommunity#225, fluttercommunity#456, flutter/flutter#149031.
e31c344 to
13e02bd
Compare
|
Re: CI status — the red checks here are pre-existing infrastructure failures, not regressions from this PR. Evidence: The jobs that actually exercise this change all pass:
The failing jobs fail identically on
The last ~6 This change is purely additive: a new, non-exported |
…allbacks PackageInfoEnvironment now resolves the web version from, in order: PACKAGE_INFO_PLUS_VERSION (explicit), FLUTTER_BUILD_NAME (flutter/flutter#187935, injected by flutter_tools like FLUTTER_APP_FLAVOR), and dart.package.version (dart-lang/sdk#38855). Once either upstream proposal lands, apps need no configuration at all.
|
Forward-compatibility note. This PR now also recognizes tool-provided version defines as fallbacks, so apps will need no configuration at all once their build front-end injects the version itself:
Resolution order is: explicit |
Description
On the web,
PackageInfo.fromPlatform()resolves the version by fetchingversion.jsonfrom the server at runtime (with a cache buster). That file reflects whatever is currently deployed — not the bundle actually executing in the browser. The two states routinely diverge:ClientException: Invalid content-length header [-1]when readingversion.jsonon a Firefox extension #3028), leaving every field empty.This makes the reported version actively misleading rather than merely missing — and the version is the whole point of the plugin. Its two canonical uses, showing the running version and checking whether an update is needed, are both defeated when the value is the server's. An update gate built on it can never fire for the very users it targets, since stale clients self-report as up to date.
This PR
Adds a small opt-in library,
package_info_plus_environment.dart, exposingPackageInfoEnvironment.packageInfo:PackageInfobuilt from compile-timePACKAGE_INFO_PLUS_*defines (flutter build web --dart-define=PACKAGE_INFO_PLUS_VERSION=1.2.3 ...). These are embedded in the running bundle and cannot diverge from it. A web build that omitsPACKAGE_INFO_PLUS_VERSIONfails to compile (akIsWeb-conditionalconstassert), so a misleading version can never ship silently.PackageInfo.fromPlatform(), which reads the installed binary and is already reliable. The defines are not required there.Design choices that keep this safe and non-breaking:
PackageInfo.fromPlatform()is untouched. No behavior change for existing users.package_info_plus_environment.dart. Apps that don't opt in are completely unaffected — no new build requirement is imposed package-wide.flutter analyzeis clean and the existing + new tests pass. The web compile-time enforcement is verified by a compilation test (a web build without the define does not compile) rather than a runtime test.Related Issues
ClientException: Invalid content-length header [-1]when readingversion.jsonon a Firefox extension #3028Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change